package com.gxuwz.stats.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxuwz.stats.entity.AchievementTransfer;
import com.gxuwz.stats.mapper.AchievementTransferMapper;
import com.gxuwz.stats.service.AchievementTransferService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class AchievementTransferServiceImpl extends ServiceImpl<AchievementTransferMapper, AchievementTransfer>
        implements AchievementTransferService {

    @Override
    @Transactional
    public List<AchievementTransfer> importExcel(MultipartFile file, boolean updateSupport) throws IOException {
        List<AchievementTransfer> list = new ArrayList<>();

        try (InputStream is = file.getInputStream();
             Workbook workbook = new XSSFWorkbook(is)) {

            Sheet sheet = workbook.getSheetAt(0);
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                if (row == null) continue;

                AchievementTransfer entity = new AchievementTransfer();
                entity.setUserId((int)row.getCell(0).getNumericCellValue());
                entity.setTransferAchievementName(row.getCell(1).getStringCellValue());
                entity.setTransferType(row.getCell(2).getStringCellValue());
                entity.setTransferAmount((int)row.getCell(3).getNumericCellValue());
                entity.setTransferScore((int)row.getCell(4).getNumericCellValue());
                entity.setTransferNotes(row.getCell(5).getStringCellValue());

                if (updateSupport) {
                    saveOrUpdate(entity);
                } else {
                    save(entity);
                }
                list.add(entity);
            }
        }
        return list;
    }

    @Override
    public byte[] exportExcel(String keyword) throws IOException {
        QueryWrapper<AchievementTransfer> wrapper = new QueryWrapper<>();
        if (keyword != null && !keyword.isEmpty()) {
            wrapper.like("transfer_achievement_name", keyword)
                    .or().like("user_id", keyword)
                    .or().like("transfer_type", keyword);
        }

        List<AchievementTransfer> list = list(wrapper);

        try (Workbook workbook = new XSSFWorkbook();
             ByteArrayOutputStream out = new ByteArrayOutputStream()) {

            Sheet sheet = workbook.createSheet("成果转化数据");

            // 创建表头
            String[] headers = {"用户ID", "成果名称", "类型", "金额(万)", "分值", "备注"};
            Row headerRow = sheet.createRow(0);
            for (int i = 0; i < headers.length; i++) {
                headerRow.createCell(i).setCellValue(headers[i]);
            }

            // 填充数据
            for (int i = 0; i < list.size(); i++) {
                AchievementTransfer item = list.get(i);
                Row row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(item.getUserId());
                row.createCell(1).setCellValue(item.getTransferAchievementName());
                row.createCell(2).setCellValue(item.getTransferType());
                row.createCell(3).setCellValue(item.getTransferAmount());
                row.createCell(4).setCellValue(item.getTransferScore());
                row.createCell(5).setCellValue(item.getTransferNotes());
            }

            workbook.write(out);
            return out.toByteArray();
        }
    }
}